package DataAccessObject;

import java.util.Map;
import javax.persistence.*;
import org.apache.openjpa.persistence.*;

public class Connection {

  private EntityManagerFactory emf;
	private EntityManager em;
	public OpenJPAEntityManager kem;

	public Connection() {
	  emf = Persistence.createEntityManagerFactory("reversemappingtool", getProperties());
	  this.newSession();
	}// Constructor

	private Map<Object, Object> getProperties() {
		Map<Object, Object> properties = System.getProperties();
		properties.put("openjpa.ConnectionURL", "jdbc:mysql://localhost:3306/p13");
		properties.put("openjpa.ConnectionUserName", "root");
		properties.put("openjpa.ConnectionPassword", "");		
		properties.put("openjpa.ConnectionDriverName", "com.mysql.jdbc.Driver");
		properties.put("openjpa.ConnectionFactoryProperties","PrettyPrint=true, PrettyPrintLineLength=72");
		return properties;
	}// getProperties
	
  public void newSession() {
    em = emf.createEntityManager();
    kem = OpenJPAPersistence.cast(em);
  }// newSession
	
	public void open() {
	  this.newSession();
	  kem.getTransaction().begin();
	}// open

	public void close() {
		if(kem != null)
			kem.close();
		kem = null;
	}// close
	
	public void exit() {
	  if(emf.isOpen())
	    emf.close();
	}// exit
  
}// class Connection

